package one.stand.service.impl;

import one.stand.advice.ResultEnum;
import one.stand.mapper.CallsPhoneRecordMapper;
import one.stand.mapper.ConsultRecordMapper;
import one.stand.mapper.DynamicForwardRecordMapper;
import one.stand.mapper.GoodsForwardRecordMapper;
import one.stand.model.*;
import one.stand.request.DynamicForwardRecordRequest;
import one.stand.request.GoodsForwardRecordRequest;
import one.stand.service.BusinessCardBaseService;
import one.stand.service.RecordService;
import one.stand.util.AssertUtil;
import one.stand.util.DateUtil;
import one.stand.util.OneListUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class RecordServiceImpl implements RecordService {

    @Autowired
    private BusinessCardBaseService businessCardBaseService;

    @Autowired
    private ConsultRecordMapper consultRecordMapper;
    /**
     * 商品分析记录
     */
    @Autowired
    private GoodsForwardRecordMapper goodsForwardRecordMapper;
    /**
     * 文章分析记录
     */
    @Autowired
    private DynamicForwardRecordMapper dynamicForwardRecordMapper;
    @Autowired
    private CallsPhoneRecordMapper callsPhoneRecordMapper;

    @Override
    @Transactional
    public void addConsultRecord(Integer userId, Integer cardId, Integer type) {
        ConsultRecordModel model = new ConsultRecordModel();
        model.setUserId(userId);
        model.setCardId(cardId);
        model.setSonsultType(type);
        List<ConsultRecordModel> list = consultRecordMapper.select(model);
        int r;
        if (OneListUtil.isEmpty(list)) {
            BusinessCard card = businessCardBaseService.getCustomer(cardId);
            model.setCompanyId(card.getCompanyId());
            model.setCreateTime(new Date());
            model.setIsDel(BaseModel.NO_DEL);
            model.setNum(1);
            r = consultRecordMapper.insertSelective(model);
        } else {
            r = consultRecordMapper.updateNum(model);
        }
        AssertUtil.isTrue(r == 1, ResultEnum.FAIL);
    }

    @Override
    public ResultModel<Integer> addGoodsForwardRecord(GoodsForwardRecordRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        GoodsForwardRecordModel model = new GoodsForwardRecordModel();
        BeanUtils.copyProperties(request, model);
        model.setUserId(request.getId().intValue());
        model.setCreateTime(DateUtil.currentDate());
        model.setIsDel(1);
        goodsForwardRecordMapper.insertSelective(model);
        return ResultModel.success(model.getForwardId());
    }

    @Override
    public ResultModel<Boolean> validGoodsForwardRecord(GoodsForwardRecordRequest request) {
        if (request == null || request.getForwardId() == null) {
            return ResultModel.checkParamFail();
        }
        GoodsForwardRecordModel model = new GoodsForwardRecordModel();
        model.setForwardId(request.getForwardId());
        model.setIsDel(0);
        goodsForwardRecordMapper.updateByPrimaryKeySelective(model);
        return ResultModel.success(true);
    }

    @Override
    public ResultModel<Boolean> addDynamicForwardRecord(DynamicForwardRecordRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        DynamicForwardRecordModel model = new DynamicForwardRecordModel();
        BeanUtils.copyProperties(request, model);
        model.setUserId(request.getId().intValue());
        model.setCreateTime(new Date());
        int r = dynamicForwardRecordMapper.insertSelective(model);
        if (r > 0) {
            return ResultModel.success(true);
        }
        return ResultModel.fail();
    }

    @Override
    @Transactional
    public void addCallsPhoneRecord(Integer userId, Integer cardId) {
        CallsPhoneRecordModel model = new CallsPhoneRecordModel();
        model.setUserId(userId);
        model.setCardId(cardId);
        model.setCreateTime(DateUtil.currentDate());
        int r = callsPhoneRecordMapper.insertSelective(model);
        AssertUtil.isTrue(r == 1, ResultEnum.FAIL);
    }
}
